<?php

declare(strict_types=1);

namespace App\Admin\Controller\Setting;

use App\Admin\Annotation\Auth;
use App\Admin\Annotation\Permission;
use App\Admin\Request\Tool\TableCreateRequest;
use App\Admin\Service\Setting\TableService;
use DI\Attribute\Inject;
use DI\DependencyException;
use DI\NotFoundException;
use Newbie\Annotation\Attributes\Route\Controller;
use Newbie\Annotation\Attributes\Route\GetMapping;
use Newbie\Annotation\Attributes\Route\PutMapping;
use Newbie\NewbieController;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use support\Response;

/**
 * 表设计器
 * Class TableController
 * @package App\Setting\Controller\Tools
 */
#[Controller(prefix: "setting/table"), Auth]
class TableController extends NewbieController {
    #[Inject]
    protected TableService $service;

    /**
     * 获取系统信息
     * @return Response
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[GetMapping("getSystemInfo")]
    public function getSystemInfo (): Response {
        $this->mine->scanModule();
        return $this->success([
            'tablePrefix' => $this->service->getTablePrefix(),
            'modulesList' => $this->mine->getModuleInfo()
        ]);
    }

    /**
     * 创建数据表
     * @param TableCreateRequest $request
     * @return Response
     * @throws DependencyException
     * @throws NotFoundException
     */
    #[PutMapping("save"), Permission("setting:table:save")]
    public function save (TableCreateRequest $request): Response {
        if ($this->service->createTable($request->validated())) {
            return $this->success();
        } else {
            return $this->error();
        }
    }
}